package com.zj.ecology;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.ImageView;

import com.zj.base.LayoutModule;
import com.zj.base.base_act.BaseActivity;
import com.zj.base.views.BaseLoadingView;
import com.zj.base.permission.annotations.CheekPermissions;
import com.zj.base.utils.DisplayUtils;
import com.zj.ecology.utils.ImageLoaderUtils;

import java.io.File;

import static com.zj.base.permission.manifests.PermissionsManifest.CAMERA;

/**
 * Created by zhaojie on 2018/5/24.
 */

@CheekPermissions(isForce = {true}, value = {CAMERA})
public class PhotoSevenTest extends BaseActivity {

    private ImageView ivPhoto;
    public static final String homePath = Environment.getExternalStorageDirectory() + File.separator + "1aacc/acca.jpg";

    @Override
    protected String setTitle() {
        return "7.0适配拍照测试";
    }

    @Override
    protected LayoutModule getContentView() {
        return new LayoutModule(R.layout.activity_phototest, false, false);
    }

    @Override
    public void initView() {
        ivPhoto = find(R.id.iv_photo);
        setImg();
    }

    @Override
    public void initListener() {
        ivPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoSystemPicIntent(PhotoSevenTest.this, 9090, homePath);
            }
        });
        hint(BaseLoadingView.DisplayMode.normal, "", false);
    }

    private void setImg() {
        ImageLoaderUtils.factoryBitmap(this, Uri.fromFile(new File(homePath))).scale(0.5f).excute(new ImageLoaderUtils.OnBitmapGotListener() {
            @Override
            public void onGet(Bitmap bitmap) {
                if (bitmap != null) {
                    ivPhoto.setImageBitmap(bitmap);
                } else {
                    ivPhoto.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ImageLoaderUtils.load(ivPhoto, homePath, DisplayUtils.dip2px(30), DisplayUtils.dip2px(30));
                        }
                    }, 200);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        setImg();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 9090) setImg();
    }

    private void gotoSystemPicIntent(Activity activity, int recode, String path) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(path);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri uriForFile = FileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".provider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);//将拍取的照片保存到指定URI
        } else {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        }
        try {
            activity.startActivityForResult(intent, recode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
